﻿/// <reference path="../Scripts/J.js" />
//拖放程序
j.Drag = new j.Class().extend({
    //拖放对象
    initialize: function(drag, options) {
        this.Drag = j.$(drag); //拖放对象
        this._x = this._y = 0; //记录鼠标相对拖放对象的位置
        this._marginLeft = this._marginTop = 0; //记录margin
        //事件对象(用于绑定移除事件)
        this._fM = j.BindAsEventListener(this, this.Move);
        this._borderWidth = j.CurrentStyle(this.Drag).borderWidth.replace("px", "") || 0;
        this._fS = j.bind(this, this.Stop);

        this.SetOption(options);

        this.Reset();
        this.onStart = this.options.onStart;
        this.onMove = this.options.onMove;
        this.onStop = this.options.onStop;

        this._Handle = j.$(this.options.Handle) || this.Drag;
        this._mxContainer = j.$(this.options.mxContainer) || null;

        this.Drag.style.position = "absolute";
        //透明
        if (j.Browser.isIE && !!this.options.Transparent) {
            //填充拖放对象
            with (this._Handle.appendChild(document.createElement("div")).style) {
                width = height = "100%"; backgroundColor = "#fff"; filter = "alpha(opacity:0)"; fontSize = 0;
            }
        }
        //修正范围
        this.Repair();
        j.addEventHandler(this._Handle, "mousedown", j.BindAsEventListener(this, this.Start));
    },
    Reset: function(options) {
        j.extend(this.options, options || {});
        this.Limit = !!this.options.Limit;
        this.mxLeft = parseInt(this.options.mxLeft);
        this.mxRight = parseInt(this.options.mxRight);
        this.mxTop = parseInt(this.options.mxTop);
        this.mxBottom = parseInt(this.options.mxBottom);

        this.LockX = !!this.options.LockX;
        this.LockY = !!this.options.LockY;
        this.Lock = !!this.options.Lock;
        this.Repair();
    },
    resetDragBox: function() {
        var top = 0, left = 0;
        if (this.options.Lock) {
            top = this.options.LockY;
            left = this.options.LockX;
        }
        top = Math.max(this.mxTop, top);
        left = Math.max(this.mxLeft, left);
        var iWidth = this.mxRight - left;
        var iHeight = this.mxBottom - top;
        var bigMin = j.math.scaleRectZoomOut(iWidth, iHeight, this.options.Ratio);
        if (this.options.Min) {
            if (bigMin.Width < this.options.minWidth) {
                bigMin.Width = this.options.minWidth;
                bigMin.Height = bigMin.Width * this.options.Ratio;
            }
        }
        if (bigMin.Width < iWidth)
            left += (iWidth - bigMin.Width) / 2;
        if (bigMin.Height < iHeight)
            top += (iHeight - bigMin.Height) / 2;
        bigMin.Width -= this._borderWidth * 2
        bigMin.Height -= this._borderWidth * 2
        this.Drag.style.width = bigMin.Width, this.Drag.style.height = bigMin.Height;

        this.Drag.style.top = top - this._marginTop, this.Drag.style.left = left - this._marginLeft;
        this.onMove();

    },
    //设置默认属性
    SetOption: function(options) {
        this.options = {//默认值
            Handle: null, //设置触发对象（不设置则使用拖放对象）
            Limit: false, //是否设置范围限制(为true时下面参数有用,可以是负数)
            mxLeft: 0, //左边限制
            mxRight: 9999, //右边限制
            mxTop: 0, //上边限制
            mxBottom: 9999, //下边限制
            mxContainer: null, //指定限制在容器内
            LockX: false, //是否锁定水平方向拖放
            LockY: false, //是否锁定垂直方向拖放
            Lock: false, //是否锁定
            Ratio: 0, //框比例
            Min: false, //是否最小宽高限制(为true时下面min参数有用)
            minWidth: 50, //最小宽度
            minHeight: 50, //最小高度
            Transparent: false, //是否透明
            onStart: function() { }, //开始移动时执行
            onMove: function() { }, //移动时执行
            onStop: function() { } //结束移动时执行
        };
        j.extend(this.options, options || {});
    },
    //准备拖动
    Start: function(oEvent) {
        if (this.Lock) { return; }
        this.Repair();
        //记录鼠标相对拖放对象的位置
        this._x = oEvent.clientX - this.Drag.offsetLeft;
        this._y = oEvent.clientY - this.Drag.offsetTop;
        //记录margin
        this._marginLeft = parseInt(j.CurrentStyle(this.Drag).marginLeft) || 0;
        this._marginTop = parseInt(j.CurrentStyle(this.Drag).marginTop) || 0;
        //mousemove时移动 mouseup时停止
        j.addEventHandler(document, "mousemove", this._fM);
        j.addEventHandler(document, "mouseup", this._fS);
        if (j.Browser.isIE) {
            //焦点丢失
            j.addEventHandler(this._Handle, "losecapture", this._fS);
            //设置鼠标捕获
            this._Handle.setCapture();
        } else {
            //焦点丢失
            j.addEventHandler(window, "blur", this._fS);
            //阻止默认动作
            oEvent.preventDefault();
        };
        //附加程序
        this.onStart();
    },
    //修正范围
    Repair: function() {
        if (this.Limit) {
            //修正错误范围参数
            this.mxRight = Math.max(this.mxRight, this.mxLeft + this.Drag.offsetWidth);
            this.mxBottom = Math.max(this.mxBottom, this.mxTop + this.Drag.offsetHeight);
            //如果有容器必须设置position为relative或absolute来相对或绝对定位，并在获取offset之前设置
            !this._mxContainer || j.CurrentStyle(this._mxContainer).position == "relative" || j.CurrentStyle(this._mxContainer).position == "absolute" || (this._mxContainer.style.position = "relative");
        }
    },
    //拖动
    Move: function(oEvent) {
        //判断是否锁定
        if (this.Lock) { this.Stop(); return; };
        //清除选择
        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
        //设置移动参数
        var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
        //设置范围限制
        if (this.Limit) {
            //设置范围参数
            var mxLeft = this.mxLeft, mxRight = this.mxRight, mxTop = this.mxTop, mxBottom = this.mxBottom;
            //如果设置了容器，再修正范围参数
            if (!!this._mxContainer) {
                mxLeft = Math.max(mxLeft, 0);
                mxTop = Math.max(mxTop, 0);
                mxRight = Math.min(mxRight, this._mxContainer.clientWidth);
                mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);
            };
            //修正移动参数
            iLeft = Math.max(Math.min(iLeft, mxRight - this.Drag.offsetWidth), mxLeft);
            iTop = Math.max(Math.min(iTop, mxBottom - this.Drag.offsetHeight), mxTop);
        }
        //设置位置，并修正margin
        if (!this.LockX) { this.Drag.style.left = iLeft - this._marginLeft + "px"; }
        if (!this.LockY) { this.Drag.style.top = iTop - this._marginTop + "px"; }
        //附加程序
        this.onMove();
    },
    //停止拖动
    Stop: function() {
        //移除事件
        j.removeEventHandler(document, "mousemove", this._fM);
        j.removeEventHandler(document, "mouseup", this._fS);
        if (j.Browser.isIE) {
            j.removeEventHandler(this._Handle, "losecapture", this._fS);
            this._Handle.releaseCapture();
        } else {
            j.removeEventHandler(window, "blur", this._fS);
        };
        //附加程序
        this.onStop();
    }
});